// import Vue from 'vue'
import router from '@/router'
// import store from '@/store'
import tipMesObj from '@/assets/json/tipMes'
/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}
export function tipMes (key) {
  return tipMesObj[key]
}
/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  // Vue.cookie.delete('token')
  // store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
  sessionStorage.clear()
  localStorage.clear();
}

export const handleEnter = (hc,id=1) => {
  document.onkeydown = (e) => {
    var event = e || window.event;
    if (event.keyCode && event.keyCode == 13&&event.srcElement.className == 'el-input__inner'&&event.srcElement.attributes.nohandleenter==undefined) {
      var lastTabID = localStorage.getItem('cuslx');
      console.log(lastTabID)
      if (!lastTabID){
        hc(id);
      }
      localStorage.setItem('cuslx', '');
      // console.log(event.srcElement.className)
    }/* else if (event.which == 13&&event.srcElement.className == 'el-input__inner'){
      hc()  ;

    }*/

  };
};
// 自定义指令
export const decimalDirective = {
  bind(el) {
    el.addEventListener('input', function(e) {
      const input = e.target
      let text = input.value.trim()
      // 如果输入以小数点开头，则在前面加上0
      if (text.startsWith('.')) {
        text = '0' + text
      }
      // 如果输入以0开头且长度大于1，并且第二位不是小数点，则去掉开头的0
      if (text.startsWith('0') && text.length > 1 && text[1] !== '.') {
        text = text.substring(1)
      }
      // 匹配小数点后最多两位数字或者零
      const regex = /^\d+(\.\d{0,2})?$/;
      if (!regex.test(text)) {
        // 如果输入不符合要求，则去掉最后一个字符
        input.value = text.substring(0, text.length - 1)
        // 触发输入框的输入事件，更新数据双向绑定
        input.dispatchEvent(new Event('input'))
      }
    })
  }
}

